package org.lex.ftp.ui.prefs;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.TableColumnModel;

import org.lex.ftp.util.prefs.FileAction;
import org.lex.swing.Swings;


public class FileRulesPane extends javax.swing.JPanel {
	private JCheckBox delayCheckBox;
	private JSpinner rollback2Spinner;
	private JCheckBox rollbackCheckBox;
	private JSpinner delaySpinner;
	private JTable table;
	private FileRulesTableModel tableModel;

	public static void main(String[] args) {
		Swings.show(new FileRulesPane(), false);
	}

	public FileRulesPane() {
		super();
		initialize();
	}

	public void restoreDefaults() {
		this.getTableModel().restoreDefaults();
	}

	public void saveConfiguration() {
		if (true == this.getTableModel().hasContentChanged())
			this.getTableModel().saveToResourceMap();
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(this.getTableModel());
			table.setRowSelectionAllowed(false);
			table.setCellSelectionEnabled(true);
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			// setup cell editor
			JComboBox cb = new JComboBox(FileAction.displayNames());
			cb.setFont(table.getFont());
			DefaultCellEditor editor = new DefaultCellEditor(cb);
			editor.setClickCountToStart(1);
			TableColumnModel model = table.getColumnModel();
			for (int i = 1; i < 4; i++)
				model.getColumn(i).setCellEditor(editor);
			table.setRowHeight(cb.getPreferredSize().height);
			// cell renderer
			LabelRenderer br = new LabelRenderer();
			br.setFont(table.getFont());
			model.getColumn(0).setCellRenderer(br);
		}
		return table;
	}

	private JCheckBox getDelayCheckBox() {
		if (delayCheckBox == null) {
			delayCheckBox = new JCheckBox();
			delayCheckBox.setText("When asked, wait specified number of seconds, then overwrite");
			delayCheckBox.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					getDelaySpinner().setEnabled(delayCheckBox.isSelected());
				}
			});
		}
		return delayCheckBox;
	}

	private JCheckBox getRollbackCheckBox() {
		if (rollbackCheckBox == null) {
			rollbackCheckBox = new JCheckBox();
			rollbackCheckBox.setText("KB to rollback on resumed downloads");
			rollbackCheckBox.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					getRollbackSpinner().setEnabled(rollbackCheckBox.isSelected());
				}
			});
		}
		return rollbackCheckBox;
	}

	private JSpinner getDelaySpinner() {
		if (delaySpinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(60, 0, Short.MAX_VALUE, 10);
			delaySpinner = new JSpinner(model);
		}
		return delaySpinner;
	}

	private JSpinner getRollbackSpinner() {
		if (rollback2Spinner == null) {
			SpinnerNumberModel model = new SpinnerNumberModel(100, 0, Short.MAX_VALUE, 50);
			rollback2Spinner = new JSpinner(model);
		}
		return rollback2Spinner;
	}

	private FileRulesTableModel getTableModel() {
		if (null == tableModel)
			tableModel = new FileRulesTableModel();
		return tableModel;
	}

	private void initialize() {
		GridBagLayout centerPaneLayout = new GridBagLayout();
		this.setLayout(centerPaneLayout);
		this.add(new JScrollPane(this.getTable()), new GridBagConstraints(0, 0, 2, 1, 10.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 11, 0), 0, 0));
		this.add(getDelaySpinner(), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0,
				0, 5), 0, 0));
		this.add(getDelayCheckBox(), new GridBagConstraints(1, 1, 1, 1, 10.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));
		this.add(getRollbackSpinner(), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(5, 0, 0, 5), 0, 0));
		this.add(getRollbackCheckBox(), new GridBagConstraints(1, 2, 1, 1, 10.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(5, 0, 0, 0), 0, 0));
	}
}
